package com.example.cs4.controller;

import com.example.cs4.model.User;
import com.example.cs4.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@Api(tags = "用户服务")
public class UserServiceController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录并返回token")
    public String login(@RequestParam String name, @RequestParam String password) {
        User user = userService.login(name, password);
        if (user != null) {
            return "登录成功";
        } else {
            return "登录失败";
        }
    }
    @GetMapping("/getUserByName")
    @ApiOperation(value = "根据用户名获取用户信息", notes = "根据用户名获取用户信息")
    public User getUserByName(String name) {
        User user = userService.getUserByName(name);
        return user;
    }
    @PostMapping("/saveUser")
    @ApiOperation(value = "增加用户信息", notes = "增加用户信息")
    public User saveUser(@RequestBody User user) {
        userService.saveUser(user);
        return user;
    }
    @PostMapping("/updateUser")
    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    public User updateUser(String name, String password) {
        User user = userService.updateUser(name, password);
        return user;
    }
    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    public User deleteUser(Integer id) {
        User user = userService.deleteUser(id);
        return user;
    }
    @PostMapping("/getPage")
    @ApiOperation(value = "分页获取用户信息", notes = "分页获取用户信息")
    public List<User> getPage(@RequestParam Integer PageNum, @RequestParam Integer PageSize){
        return userService.getPage(PageNum, PageSize);
    }
    @PostMapping("/getAll")
    @ApiOperation(value = "模糊查询", notes = "获取对应字段用户信息")
    public List<User> getAll( @RequestParam String name){
        return userService.getAll(name);
    }
}
